package com.bidevalution.business.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.io.ResolverUtil.IsA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.bidevalution.business.function.FunctionCalculator;
import com.bidevalution.business.service.BeSectionButtonService;
import com.bidevalution.business.service.BeSectionParameterService;
import com.bidevalution.dao.entity.BeSectionButton;
import com.bidevalution.dao.entity.BeSectionButtonKey;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.BeSectionParameter;
import com.bidevalution.dao.mapper.BeSectionButtonMapper;
import com.bidevalution.service.layout.FormControl;
import com.bidevalution.support.exception.BizException;
import com.bidevalution.support.util.LogUtil;
import com.bidevalution.support.util.StringUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
public class BeSectionButtonServiceImpl implements BeSectionButtonService {
	@Autowired
	private BeSectionButtonMapper beSectionButtonMapper;

	@Autowired
	private BeSectionParameterService beSectionParameterService;
	private LogUtil log = LogUtil.getLogManager(getClass());

	@Override
	public int updateByPrimaryKeySelective(BeSectionButton record) {
		return beSectionButtonMapper.updateByPrimaryKey(record);

	}

	@Override
	public BeSectionButton selectByPrimaryKey(String sectionStepId, String controlCode, String layoutCode) {
		BeSectionButtonKey beSectionButtonKey = new BeSectionButtonKey();
		beSectionButtonKey.setSectionStepId(sectionStepId);
		beSectionButtonKey.setControlCode(controlCode);
		beSectionButtonKey.setLayoutCode(layoutCode);
		return beSectionButtonMapper.selectByPrimaryKey(beSectionButtonKey);
	}

	@Override
	public List<BeSectionButton> selectList(String sectionStepId, String layoutCode) {
		// TODO Auto-generated method stub
		return beSectionButtonMapper.selectList(sectionStepId, layoutCode);
	}
	@Override
	public List<FormControl> getFormControlList(BeSectionExpertInfo beSectionExpertInfo, String sectionId,
			String sectionStepId, String layoutCode) throws Exception {
		String buttonClickFunction = "";
		String permissionConfig = "";
		String controlValue;
		FormControl formControl;
		Map<String, Object> computeMap;
		List<FormControl> formControlList = Lists.newArrayList();
		for (BeSectionButton beSectionButton : selectList(sectionStepId, layoutCode)) {
			controlValue = beSectionButton.getControlValue();
			if (StringUtils.isEmpty(controlValue)) {
				continue;
			}
			computeMap = Maps.newHashMap();
			for (BeSectionParameter beSectionParameter : beSectionParameterService.selectList(sectionId)) {
				computeMap.put("$PARAM_" + beSectionParameter.getParamId(), beSectionParameter.getParamValue());
			}
			computeMap.put("$EXPERT_TYPE", beSectionExpertInfo.getExpertType());
			computeMap.put("$IS_MANAGER", beSectionExpertInfo.getIsManager());
			computeMap.put("sectionId", sectionId);
			List<BeSectionParameter> beSectionParameterList = beSectionParameterService.selectList(sectionId);
			// 替换头部区域控件的参数值
			if (controlValue.indexOf("{") != -1 && controlValue.indexOf("}") != -1) {
				for (BeSectionParameter beSectionParameter : beSectionParameterList) {
					controlValue = controlValue.replaceAll("\\{PARAM_" + beSectionParameter.getParamId() + "\\}",
							beSectionParameter.getParamValue());
				}
				
			}
			controlValue = controlValue.trim();
			//TODO 后期需要优化
			if (controlValue.toLowerCase().startsWith("{mel[")) {
				controlValue = controlValue.substring(5, controlValue.length() - 2);
				controlValue = FunctionCalculator.computeMVEL(controlValue, computeMap);
			} else if(controlValue.toLowerCase().startsWith("{ael[")){
				controlValue = controlValue.substring(5, controlValue.length() - 2);
				controlValue = FunctionCalculator.computeAviator(controlValue, computeMap);
			}
			// 替换头部区域控件单击事件的参数值
			buttonClickFunction = beSectionButton.getClickFunction();
			if (!StringUtils.isEmpty(buttonClickFunction)) {
				if (buttonClickFunction.indexOf("{") != -1 && buttonClickFunction.indexOf("}") != -1) {
					for (BeSectionParameter beSectionParameter : beSectionParameterList) {
						buttonClickFunction = buttonClickFunction.replaceAll(
								"\\{PARAM_" + beSectionParameter.getParamId() + "\\}",
								beSectionParameter.getParamValue());
					}
				}

			}
			// 生成头部区域配置对象
			formControl = new FormControl(beSectionButton.getControlCode(), controlValue,
					beSectionButton.getControlType(), String.valueOf(beSectionButton.getWidth()), buttonClickFunction,
					"1".equals(beSectionButton.getIsDisabled()) ? true : false, beSectionButton.getDisabledTip());
			// 评标参数的值放入到公式计算参数中
			

			// 计算字段权限
			permissionConfig = beSectionButton.getPermissionConfig();
			if (!StringUtils.isEmpty(permissionConfig)) {
				Map<String,Object> controlValueMap = Maps.newHashMap();
				controlValueMap.put("value", formControl.getControlValue());
				controlValueMap.put("disabled", formControl.getDisabled());
				controlValueMap.put("disabledTip", formControl.getDisabledTip());
				controlValueMap.put("isDisplay", "1");
				controlValueMap.putAll(computeMap);
				computeMap.putAll(BeanUtils.describe(formControl));
				permissionConfig = permissionConfig.trim();
				if (permissionConfig.toLowerCase().startsWith("{mel[")) {
					permissionConfig = permissionConfig.substring(5, permissionConfig.length() - 2);
					permissionConfig = FunctionCalculator.computeMVEL(permissionConfig, controlValueMap);
				} else if(permissionConfig.toLowerCase().startsWith("{ael[")){
					permissionConfig = permissionConfig.substring(5, permissionConfig.length() - 2);
					permissionConfig = FunctionCalculator.computeAviator(permissionConfig, controlValueMap);
				}else {
					throw new BizException("计算公式格式错误:" + permissionConfig);
				}
				//赋最新值
				BeanUtils.setProperty(formControl, "controlValue", controlValueMap.get("value"));
				BeanUtils.setProperty(formControl, "disabled", controlValueMap.get("disabled"));
				BeanUtils.setProperty(formControl, "disabledTip", controlValueMap.get("disabledTip"));
				//是否显示
				if("1".equals(controlValueMap.get("isDisplay"))) {
					// 把头部区域的数据加入到返回值中
					formControlList.add(formControl);
				}
				
			}else {
				// 把头部区域的数据加入到返回值中
				formControlList.add(formControl);
			}

			
		}
		return formControlList;
	}

}
